﻿#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>

bool isleap(int year) {
    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

int dayOfWeek(int y, int m) {
    int h, K, J;
    if (m < 3) {
        m += 12;
        y -= 1;
    }
    K = y % 100;  
    J = y / 100;  
    h = (1 + 13 * (m + 1) / 5 + K + K / 4 + J / 4 - 2 * J) % 7;
    return (h + 6) % 7;
}

int len_of_month(int year, int month) {
    if (month == 4 || month == 6 || month == 9 || month == 11)
        return 30;
    else if (month == 2)
        return isleap(year) ? 29 : 28;
    else
        return 31;
}

int main()
{
    int year=-1, month;
    while (year!=0)
    {
        printf("请输入年和月(输入年为0结束)\n");
        scanf("%d %d", &year, &month);
        if (year == 0)
        {
            return 0;
        }
        int day = len_of_month(year, month);
        printf("====================\n");
        printf("     %d年%d月\n", year, month);
        printf("====================\n");
        printf("一 二 三 四 五 六 日\n");

        for (int i = 0; i < 3 * dayOfWeek(year, month) - 3; i++)
        {
            printf(" ");
        }
        int count = dayOfWeek(year, month);
        for (int i = 1; i <= day; i++, count++)
        {
            printf("%-3d", i);
            if (count == 7)
            {
                printf("\n");
                count = 0;
            }
        }
        printf("\n");
    }
    return 0;
}